package com.itheima.mq.rabbitmq.configuration;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;

/**
 * 测试Direct类型的交换机
 */
// @Component
public class RabbitmqDlxExchangeConfiguration {
    
    // 声明ttl的队列
    @Bean(name = "direct.queue_03")
    public Queue commonQueue03() {
        QueueBuilder durable = QueueBuilder.durable("direct.queue_03");
        //设置队列过期时间,为30
        durable.ttl(30000);
        return durable.build();
    }
    
    //完成交换机绑定
    @Bean
    public Binding directQueueBindToFanoutQueue03(
            @Qualifier(value = "direct.exchange") Exchange exchange,
            @Qualifier(value = "direct.queue_03") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("ttl").noargs();
    }
    //-------------------------死信交换机-------------------------------
    
    //完成交换机绑定
    @Bean
    public Binding directQueueBindToFanoutQueue04(
            @Qualifier(value = "direct.exchange") Exchange exchange,
            @Qualifier(value = "direct.queue_05") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("dlx").noargs();
    }
    
    // 死信交换机发送给这个目标队列
    @Bean(name = "direct.queue_05")
    public Queue commonQueue05() {
        QueueBuilder durable = QueueBuilder.durable("direct.queue_05");
        durable.deadLetterExchange("dlx.exchange");
        durable.ttl(30000);
        durable.deadLetterRoutingKey("dlx");
        return durable.build();
    }
    
    //死信交换机,DLX DEAD-LETTER-EXCHANGE,这个消息发送给死信交换机,进而被消费,实现延迟发送效果
    // 声明死信队列
    @Bean(name = "dlx.queue")
    public Queue commonQueue04() {
        QueueBuilder durable = QueueBuilder.durable("dlx.queue");
        return durable.build();
    }
    
    // 声明死信交换机
    @Bean(name = "dlx.exchange")
    public Exchange dlxExchange() {
        ExchangeBuilder exchangeBuilder = ExchangeBuilder.directExchange("dlx.exchange");
        return exchangeBuilder.durable(true).build();
    }
    
    //完成交换机绑定
    @Bean
    public Binding dlxQueue2DlxExchange(
            @Qualifier(value = "dlx.exchange") Exchange exchange,
            @Qualifier(value = "dlx.queue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("dlx").noargs();
    }
    
}
